
package com.qf.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.qf.dao.UserDao;
import com.qf.model.User;
import com.qf.utils.C3P0Util;

public class UserDaoImpl implements UserDao{

	public User findByName(String uname) {
		QueryRunner qr = new QueryRunner(C3P0Util.getDatasource());
		User u=null;
		try {
			 u = qr.query("select * from user where uname=?", new BeanHandler<>(User.class),uname);
		} catch (SQLException e) {
			
			//TODO
			e.printStackTrace();
				
		}
		return u;
	}
    @Override
	public int add(String sql, Object...params) {
		QueryRunner qr = new QueryRunner(C3P0Util.getDatasource());
		int i=0;
		try {
			i = qr.update(sql, params);
		} catch (SQLException e) {
			
			//TODO
			e.printStackTrace();
				
		}
		//TODO
		return i;
			
	}
    
    @Override
	public  List<User> findother(String sql, Object...params) {
		
		QueryRunner qr = new QueryRunner(C3P0Util.getDatasource());
		List<User> list = new ArrayList<>();
			try {
				list=qr.query(sql, new BeanListHandler<>(User.class),params);
			} catch (SQLException e) {
				
				//TODO
				e.printStackTrace();
					
			}
		return list;
			
	}
    @Override
	public int update(String uid) {
		QueryRunner qr = new QueryRunner(C3P0Util.getDatasource());
		int i=0;
		try {
			i=qr.update("update user set state=?,code=? where uid=?","1",null,uid );
		} catch (SQLException e) {
			
			//TODO
			e.printStackTrace();
				
		}
		
		//TODO
		return i;
			
	}
    @Override
	public User findNameandPwd(String uname, String upwd) {
		QueryRunner qr = new QueryRunner(C3P0Util.getDatasource());
		User u = null;
		try {
			u=qr.query("select * from user where uname=? and upwd=?", new BeanHandler<>(User.class),uname,upwd);
		} catch (SQLException e) {
			
			//TODO
			e.printStackTrace();
				
		}
		//TODO
		return u;
			
	}
   
    
 

}

	